package ai.people.core.grpc.config;

import ai.people.core.grpc.grpcinterceptor.GrpcClientTokenInterceptor;
import ai.people.core.grpc.handler.GrpcExceptionHandler;
import net.devh.boot.grpc.server.security.authentication.CompositeGrpcAuthenticationReader;
import net.devh.boot.grpc.server.security.authentication.GrpcAuthenticationReader;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.security.oauth2.provider.token.TokenStore;

import java.util.ArrayList;
import java.util.List;

/**
 * grpc读者身份验证配置
 *
 * @author yuanqinglong
 * @date 2022/6/4 14:23
 */
@Import(value = {GrpcExceptionHandler.class, GrpcClientTokenInterceptor.class})
@ConditionalOnClass(name = "org.springframework.security.oauth2.provider.token.TokenStore")
public class GrpcConfiguration {
    //
    //@Bean
    //AuthenticationManager authenticationManager() {
    //    final List<AuthenticationProvider> providers = new ArrayList<>();
    //    providers.add(new DaoAuthenticationProvider()); // Possibly DaoAuthenticationProvider
    //    return new ProviderManager(providers);
    //}
    @Bean
    public GrpcAuthenticationReader reader(TokenStore redisTokenStore) {
        final List<GrpcAuthenticationReader> readers = new ArrayList<>();
        readers.add( new RedisGrpcAuthenticationReader(redisTokenStore));
        return new CompositeGrpcAuthenticationReader(readers);
    }
}
